package com.sens.network;

import android.content.Context;

import com.sens.bean.BaseJson;
import com.sens.common.JsonUtils;
import com.sens.database.LiteOrmDBUtil;
import com.sens.tables.AlipayOrder;
import com.yanzhenjie.nohttp.rest.Response;

public class AlipayOrderCallback extends ResponseCallback<BaseJson> {
    private String tradeNo;
    private Context context;
    private int retryCount;
    private boolean needRetry = true;

    public AlipayOrderCallback(Context context, String tradeNo) {
        this.context = context;
        this.tradeNo = tradeNo;
    }

    public AlipayOrderCallback(Context context, String tradeNo, boolean needRetry) {
        this.context = context;
        this.tradeNo = tradeNo;
        this.needRetry = needRetry;
    }

    @Override
    public void onSucceed(int what, Response<BaseJson> response) {
        super.onSucceed(what, response);
        BaseJson result = response.get();
        if (result != null && result.getStatus() == 1) {
            AlipayOrder alipayOrder = LiteOrmDBUtil.getGlobalDBUtil(context).queryOneByWhere(AlipayOrder.class, "tradeNo", tradeNo);
            if (alipayOrder == null) return;
            alipayOrder.setHasUpload(true);
            LiteOrmDBUtil.getGlobalDBUtil(context).update(alipayOrder);
            onComplete(true, tradeNo, JsonUtils.parserBean2Json(result));
            tradeNo = null;
            context = null;
        } else {
            onFailed(what, response);
        }
    }

    @Override
    public void onFailed(int what, Response<BaseJson> response) {
        super.onFailed(what, response);
        if (!needRetry || retryCount > 1) {
            BaseJson result = response.get();
            //TODO 不上传日志
//            if (result != null) {
//                AlipayOrder alipayOrder = LiteOrmDBUtil.getGlobalDBUtil(context).queryOneByWhere(AlipayOrder.class, "tradeNo", tradeNo);
//                if (alipayOrder != null)
//                    ApiByHttp.getInstance().uploadAlipayLog(alipayOrder, response.get(), new AlipayLogCallback(context, alipayOrder, result));
//            }
            onComplete(false, tradeNo, JsonUtils.parserBean2Json(result));
            tradeNo = null;
            context = null;
            return;
        }
        retryCount++;
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    AlipayOrder alipayOrder = LiteOrmDBUtil.getGlobalDBUtil(context).queryOneByWhere(AlipayOrder.class, "tradeNo", tradeNo);
                    if (alipayOrder == null) return;
                    if (!alipayOrder.isHasUpload())
                        ApiByHttp.getInstance().uploadAlipayOrder(alipayOrder, AlipayOrderCallback.this);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    protected void onComplete(boolean isSuccess, String tradeNo, String msg) {
    }
}